import random
import matplotlib.pyplot as plt
import numpy as np

import random

def update_u(u, x):
    a, b = 3.57, 4.0
    return (a + (b-a) * (u - a + x) % (b - a)) % b

def generate_random_number():
    key = []
    u = 3.95
    x = random.random()
    for i in range(50000):
        u = update_u(u, x)
        x = random.random()
        print(u)
        key.append(u)
    return key
            


def showChart(data):
    # 计算概率分布
    counts, bins = np.histogram(data, bins=200)
    probs = counts / sum(counts) * 100

    # 绘制概率分布图
    plt.bar(bins[:-1], probs, width=0.004, alpha=0.7)

    plt.ylim([0, 8.0])
    # plt.yticks([0,0.5,1.0,1.5,2.0])
    plt.yticks([0,1,2,3,4,5,6,7,8])

    plt.xlim([3.57, 4.0])
    # plt.xticks([0,25,50,75,100,125,150,175,200,225,250])
    plt.xticks([3.57,3.7,3.8,3.9,4.0])

    # 添加标题和标签
    plt.title('title')
    plt.xlabel('x')
    plt.ylabel('Probability (%)')

    # 显示图像
    plt.show()


def seed_to_r():
    import random

def generate_random_num(seed):
    for i in (10000):
        random.seed(seed)
        u =  3.7 + 0.3 * random.random()
        print(u)


key = generate_random_number()
print(key)
showChart(key)
# generate_random_num(123)